<?php
namespace Admin\Controller;
use Think\Controller;
class ProductionorderController extends Controller {
    public function index(){

        $this->checkSession();
        //价钱
        $this->production_price();
        //库存量
        $water_number=M('production')->where("production_name='纯净水'")->field('production_number')->select();
        $noodles_number=M('production')->where("production_name='方便面'")->field('production_number')->select();
        $toothpaste_number=M('production')->where("production_name='牙膏'")->field('production_number')->select();
        $brush_number=M('production')->where("production_name='牙刷'")->field('production_number')->select();
        $towel_number=M('production')->where("production_name='毛巾'")->field('production_number')->select();
        $this->assign('water_number',$water_number[0]{'production_number'});
        $this->assign('noodles_number',$noodles_number[0]{'production_number'});
        $this->assign('toothpaste_number',$toothpaste_number[0]{'production_number'});
        $this->assign('brush_number',$brush_number[0]{'production_number'});
        $this->assign('towel_number',$towel_number[0]{'production_number'});

        $this->display();
    }
    public function production_price(){

        $water_price=M('production')->where("production_name='纯净水'")->field('production_price')->select();
        $noodles_price=M('production')->where("production_name='方便面'")->field('production_price')->select();
        $toothpaste_price=M('production')->where("production_name='牙膏'")->field('production_price')->select();
        $brush_price=M('production')->where("production_name='牙刷'")->field('production_price')->select();
        $towel_price=M('production')->where("production_name='毛巾'")->field('production_price')->select();
        $this->assign('water_price',$water_price[0]{'production_price'});
        $this->assign('noodles_price',$noodles_price[0]{'production_price'});
        $this->assign('toothpaste_price',$toothpaste_price[0]{'production_price'});
        $this->assign('brush_price',$brush_price[0]{'production_price'});
        $this->assign('towel_price',$towel_price[0]{'production_price'});
    }
    public function  info()
    {
        $this->checkSession();
        //商品价钱
        $water_price = M('production')->where("production_name='纯净水'")->field('production_price')->select();
        $noodles_price = M('production')->where("production_name='方便面'")->field('production_price')->select();
        $toothpaste_price = M('production')->where("production_name='牙膏'")->field('production_price')->select();
        $brush_price = M('production')->where("production_name='牙刷'")->field('production_price')->select();
        $towel_price = M('production')->where("production_name='毛巾'")->field('production_price')->select();

        $now = date('Y-m-d');
        $room_id = $_POST['room_id'];

        $u_id1 = M('room_order')->where("room_id='$room_id'and starttime='$now'and instatus='已入住'")->field('user_id')->select();//入住当天
        $u_id2 = M('room_order')->where("room_id='$room_id'and endtime='$now'and instatus='已入住'")->field('user_id')->select();//退房当天
        $user_id1 = $u_id1[0]{'user_id'};//入住当天客户号
        $user_id2 = $u_id2[0]{'user_id'};//退房当天客户号


        $water_sum = $_POST['water'];
        $noodles_sum = $_POST['noodles'];
        $toothpaste_sum = $_POST['toothpaste'];
        $brush_sum = $_POST['brush'];
        $towel_sum = $_POST['towel'];

        //商品订单总价
        $production_sum = (int)$water_sum * (double)$water_price[0]{'production_price'} + (int)$noodles_sum * $noodles_price[0]{'production_price'} + (int)$toothpaste_sum * $toothpaste_price[0]{'production_price'} + (int)$brush_sum * $brush_price[0]{'production_price'} + (int)$towel_sum * $towel_price[0]{'production_price'};




        //库存量
        $water_number=M('production')->where("production_name='纯净水'")->field('production_number')->select();
        $noodles_number=M('production')->where("production_name='方便面'")->field('production_number')->select();
        $toothpaste_number=M('production')->where("production_name='牙膏'")->field('production_number')->select();
        $brush_number=M('production')->where("production_name='牙刷'")->field('production_number')->select();
        $towel_number=M('production')->where("production_name='毛巾'")->field('production_number')->select();
        $a=$water_number[0]{'production_number'};
        $b=$noodles_number[0]{'production_number'};
        $c=$toothpaste_number[0]{'production_number'};
        $d=$brush_number[0]{'production_number'};
        $e=$towel_number[0]{'production_number'};

        $water_sum = $_POST['water'];
        $noodles_sum = $_POST['noodles'];
        $toothpaste_sum = $_POST['toothpaste'];
        $brush_sum = $_POST['brush'];
        $towel_sum = $_POST['towel'];


        $water_difference=(int)$a-(int)$water_sum;
        $noodles_difference=(int)$b-(int)$noodles_sum;
        $toothpaste_difference=(int)$c-(int)$toothpaste_sum;
        $brush_difference=(int)$d-(int)$brush_sum;
        $towel_difference=(int)$e-(int)$towel_sum;



        /*if ($result !== false) {
            $this->success('办理入住成功！',U('roomorder/index'));
        } else {
            $this->error('已经办理入住了！',U('roomorder/index'));
        }*/


        if($water_sum<=0&&$noodles_sum<=0&&$toothpaste_sum<=0&&$brush_sum<=0&&$towel_sum<=0) {
            $this->error('商品数量请填写正确！');
        }

        if($water_difference<0|$noodles_difference<0|$toothpaste_difference<0|$brush_difference<0|$toothpaste_difference<0){
            $this->error("库存量不足，请参考库存量下单！");
        }else {
            //纯净水
            $data5['production_number']=$water_difference;
            $condition5['production_name'] ="纯净水";
            $result5 = M('production')->where($condition5)->data($data5)->save();
            //方便面
            $data1['production_number'] = $noodles_difference;
            $condition1['production_name'] = "方便面";
            $result1 = M('production')->where($condition1)->data($data1)->save();
            //牙膏
            $data2['production_number'] = $toothpaste_difference;
            $condition2['production_name'] = "牙膏";
            $result2 = M('production')->where($condition2)->data($data2)->save();
            //牙刷
            $data3['production_number'] = $brush_difference;
            $condition3['production_name'] = "牙刷";
            $result3 = M('production')->where($condition3)->data($data3)->save();
            //毛巾
            $data4['production_number'] = $towel_difference;
            $condition4['production_name'] = "毛巾";
            $result4 = M('production')->where($condition4)->data($data4)->save();
        }




     if (!empty($user_id1)) {//用户是否存在
            $data['user_id'] = $user_id1;
            $data{'description'} = "纯净水$water_sum 件，方便面$noodles_sum 件，牙膏$toothpaste_sum 件，牙刷$brush_sum 件，毛巾$towel_sum 件。";
            $data['pay_time'] = date("y-m-d:H:i:s");
            $data['sum'] = $production_sum;
            $data['operator']=$_SESSION['employe_id'];

            if ($p_order_id = M('production_order')->lock(true)->add($data)) {// 插入商品订单表
                $stat['p_order_id'] = $p_order_id;

                if ($p_order_id) {
                    $data['time']=date("Y-m-d H-m-s");
                    $data['operation']="客房售卖商品";
                    $data['operator']=$_SESSION['employe_id'];
                    $data['order_id']=$p_order_id;
                    $data['room_id']=$room_id;
                    M('log')->add($data);
                    $this->success('购买成功');;;
                } else {
                    $this->error('购买失败');
                }
            }
        }
        else if(!empty($user_id2)){
            $data['user_id'] = $user_id2;
            $data{'description'} = "纯净水$water_sum 件，方便面$noodles_sum 件，牙膏$toothpaste_sum 件，牙刷$brush_sum 件，毛巾$towel_sum 件。";
            $data['pay_time'] = date("y-m-d:H:i:s");
            $data['sum'] = $production_sum;
            $data['operator']=$_SESSION['employe_id'];

            if ($p_order_id = M('production_order')->lock(true)->add($data)) {// 插入商品订单表
                $stat['p_order_id'] = $p_order_id;

                if ($p_order_id) {
                    $data['time']=date("Y-m-d H-m-s");
                    $data['operation']="客房售卖商品";
                    $data['operator']=$_SESSION['employe_id'];
                    $data['order_id']=$p_order_id;
                    $data['room_id']=$room_id;
                    M('log')->add($data);
                    $this->success('购买成功');;;
                } else {
                    $this->error('购买失败');
                }
            }
        }
        else {
            $this->error("该房间无人入住，请重新输入！");
        }
    }
    public function checkSession(){
        $employe_id = $_SESSION['employe_id'];
        if(isset($employe_id)){
            $this->assign('employe_id',$employe_id);
        }else{
            $this->error("需先登录,方可进行查询！",U('Login/index'));
        }
    }
}